package com.sanhe.rfmuscle.pipe;

import static com.menu.app.util.FunctionCode.START1;
import static com.menu.app.util.FunctionCode.START2;

import com.menu.app.it.RentListener;
import com.menu.app.util.pipe.BasePortEventPipe;
import com.menu.app.util.port.SerialManage;
import com.sanhe.rfmuscle.event.Event;
import com.sanhe.rfmuscle.event.Num1Event;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class PortPipe extends BasePortEventPipe {

    static PortPipe instance;

    public static PortPipe getInstance() {
        if (instance == null) {
            instance = new PortPipe();
        }
        return instance;
    }

    private PortPipe() {
        super();
    }

    @Override
    public void doInit() {
        super.doInit();
        isStart = false;
    }

    @Override
    public boolean setRent(boolean on, long value, RentListener listener) {
        boolean bo = super.setRent(on, value, listener);
        if (!bo) {
            return false;
        }
        try {
            long delay = 200, okDelay = 500;
            if (on) {
                sendTouchEvent((byte) 1, (byte) 0x1a);//to rent set
                Thread.sleep(delay);
                if (Event.rentEvent.getValue() == 1) {
                    ;
                } else {
                    String s = "158111";
                    for (char c : s.toCharArray()) {//input pwd
                        sendTouchEvent((byte) 0, (byte) (0x60 + (c - '0')));
                        Thread.sleep(delay);
                    }
                    sendTouchEvent((byte) 1, (byte) 0x6c);//ok
                    Thread.sleep(okDelay);
                    sendTouchEvent((byte) 1, (byte) 0x1b);//rent on
                    Thread.sleep(delay);
                    sendTouchEvent((byte) 1, (byte) 0x44);//ok
                    Thread.sleep(okDelay);
                    sendTouchEvent((byte) 1, (byte) 0x20);//exit
                    Thread.sleep(delay);
                    sendTouchEvent((byte) 1, (byte) 0x1a);//to rent set
                    Thread.sleep(delay);
                }

                String s = "158158";
                for (char c : s.toCharArray()) {//input pwd
                    sendTouchEvent((byte) 0, (byte) (0x60 + (c - '0')));
                    Thread.sleep(delay);
                }
                sendTouchEvent((byte) 1, (byte) 0x6c);//ok
                Thread.sleep(okDelay);

                s = value + "";
                for (char c : s.toCharArray()) {//input point
                    sendTouchEvent((byte) 0, (byte) (0x60 + (c - '0')));
                    Thread.sleep(delay);
                }
                sendTouchEvent((byte) 1, (byte) 0x6c);//ok
                Thread.sleep(okDelay);
            } else {
                if (Event.rentEvent.getValue() == 1) {
                    sendTouchEvent((byte) 1, (byte) 0x1a);//to rent set
                    Thread.sleep(delay);
                    String s = "158111";
                    for (char c : s.toCharArray()) {//input pwd
                        sendTouchEvent((byte) 0, (byte) (0x60 + (c - '0')));
                        Thread.sleep(delay);
                    }
                    sendTouchEvent((byte) 1, (byte) 0x6c);//ok
                    Thread.sleep(okDelay);
                    sendTouchEvent((byte) 1, (byte) 0x1c);//rent on
                    Thread.sleep(delay);
                    sendTouchEvent((byte) 1, (byte) 0x44);//ok
                    Thread.sleep(okDelay);
                    sendTouchEvent((byte) 1, (byte) 0x20);//exit
                    Thread.sleep(delay);
                }
            }
            for (RentListener l : rentListener) {
                l.onSetRent(true);
            }
        } catch (Throwable e) {
            e.printStackTrace();
            for (RentListener l : rentListener) {
                l.onSetRent(false);
            }
        }
        rentListener.clear();
        return true;
    }

    @Override
    protected void sendTouchEvent(byte data_H, byte data_L) {
        byte[] tmp = {START1, START2, 6, -125, 16, 1, 1, 0, data_L};
        SerialManage.getInstance().send(tmp);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Num1Event event) {
        doInit();
    }
}
